package appli;

import gmusic.bo.ReleaseBean;
import gmusic.bo.TypeBean;
import gmusic.services.impl.GMusicServicesImpl;
import gmusic.vo.ArtistVO;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import algutil.date.ConversionDate;
import algutil.system.Systeme;

public class ExportReleasesFromGMusic {

	private final static Logger log = Logger
			.getLogger(ExportReleasesFromGMusic.class);

	private static List<TypeBean> types;

	public static void main(String[] args) throws Exception {

		if (args.length != 1) {
			log.error("Usage ExportReleasesFromGMusic <path_fichier_export>");

			return;
		}
		File path = new File(args[0]);
		if (!path.exists() || !path.isDirectory()) {
			log.error("Le parametre '" + path
					+ "' n'est pas un repertoire ou n'existe pas.");
			return;
		}

		DOMConfigurator.configure("conf/log4j.xml");
		if (Systeme.getOS() == Systeme.WINDOWS_OS) {
			Properties systemProperties = System.getProperties();
			systemProperties.setProperty("http.proxyHost", "172.16.1.138");
			systemProperties.setProperty("http.proxyPort", "3128");
		}

		types = GMusicServicesImpl.getInstance().getTypes();
		// List<ArtistBean> artists =
		// GMusicServicesImpl.getInstance().getAllArtists();
		List<ArtistVO> artistsVo = GMusicServicesImpl.getInstance()
				.getAllArtistsWithCountry();
		ArtistVO a = null;

		FileWriter fw = new FileWriter(new File(path + File.separator
				+ "export_releases_" + ConversionDate.formatYYYYMMDD()
				+ ".html"));
		PrintWriter writer = new PrintWriter(fw, true);

		ecrireHeader(writer);

		String currentLetter = "aa";

		List<String> lettres = GMusicServicesImpl.getInstance()
				.getPremieresLettreArtistes();

		writer.println("<a name=\"start\">Lettre : </a>");

		for (int k = 0; k < lettres.size(); k++) {
			writer.println("<a href=\"#" + lettres.get(k).toUpperCase() + "\">"
					+ lettres.get(k).toUpperCase() + "</a> ");

		}

		for (int k = 0; k < artistsVo.size(); k++) {

			if (!artistsVo.get(k).getNom().substring(0, 1).equalsIgnoreCase(
					currentLetter)) {
				
				if (!currentLetter.equalsIgnoreCase("aa")) {
					writer
							.println("<a href=\"#start\"><small>start</small></a>");
				}
				
				currentLetter = artistsVo.get(k).getNom().substring(0, 1)
						.toUpperCase();

				writer.println("<h1><a name=\"" + currentLetter + "\">"
						+ currentLetter + "</a></h1>");

			}

			// for (int k = 0; k < 11; k++) {
			a = artistsVo.get(k);
			log.info(a);
			writer.println("<b>" + a + "</b>");
			writer.println("<ul>");

			List<ReleaseBean> l = GMusicServicesImpl.getInstance().getReleases(
					a.getAgmid());

			for (int i = 0; i < l.size(); i++) {
				if (l.get(i).getTypeId() == 1) {
					writer.println("<li><b>" + l.get(i).getAnnee() + " "
							+ l.get(i).getNom() + "</b> <small>["
							+ getTypeName(l.get(i).getTypeId())
							+ "]</small></li>");
				} else if (l.get(i).getTypeId() == 0) {
					writer.println("<li>" + l.get(i).getAnnee() + " "
							+ l.get(i).getNom() + " <small>["
							+ getTypeName(l.get(i).getTypeId())
							+ "]</small></li>");
				} else {
					writer.println("<li><i>" + l.get(i).getAnnee() + " "
							+ l.get(i).getNom() + "</i> <small>["
							+ getTypeName(l.get(i).getTypeId())
							+ "]</small></li>");
				}
			}
			writer.println("</ul>");

		}

		ecrireFooter(writer);
		writer.close();
		fw.close();
	}

	private static String getTypeName(int typeId) {
		for (int j = 0; j < types.size(); j++) {
			if (types.get(j).getId() == typeId) {
				return types.get(j).getNom();
			}
		}
		return "Undefined";
	}

	private static void ecrireHeader(PrintWriter w) {
		w.println("<head>");
		w.println("<title>Releases list</title>");
		w.println("</head>");
		w.println("<body>");
	}

	private static void ecrireFooter(PrintWriter w) {
		w.println("</body>");
		w.println("</html>");
	}
}
